<template>
	<view>
		<navbar :isback="true" title="登录"></navbar>
		<view class="con">
			<image src="/static/logo.png" class="con__logo" mode=""></image>
			<view class="con__text">
				驼乡生活信息超市
			</view>
			<view class="con__btn">
				<u-button text="手机号一键登录" v-if="!isgx" @click="nogx" color="linear-gradient(87deg, #3494FF, #3989FF)"
					:customStyle="{height:'80rpx',}"></u-button>
				<u-button text="手机号一键登录" v-else @getphonenumber="getphonenumber" openType="getPhoneNumber"
					color="linear-gradient(87deg, #3494FF, #3989FF)" :customStyle="{height:'80rpx',}"></u-button>
				<view class="" style="height: 50rpx;width: 650rpx;">

				</view>
				<!-- <u-button text="其它手机号登录" @click="clickqt" plain  :customStyle="{height:'80rpx',}"></u-button> -->
			</view>
			<view class="u-flex u-flex-xy-center">
				<view class="" @click="isgx=!isgx">
					<u-icon name="/static/my/yx.png" size="40rpx" v-if="isgx"></u-icon>
					<u-icon name="/static/my/wx.png" size="40rpx" v-else></u-icon>
				</view>
				<view class="con__text">
					我已阅读并同意 <text style="color: #0384fc;" @click="to('/pages/my/protocol')">用户服务协议、隐私政策</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import user from '@/api/_user.js'
	export default {
		data() {
			return {
				isgx: false
			};
		},
		methods: {
			getphonenumber(e) {
				let _this = this
				// console.log(_this.$store.commit('getuserifo'))
				console.log(e)
				uni.login({
					"provider": "weixin",
					"onlyAuthorize": true, // 微信登录仅请求授权认证
					success: function(loginRes) {
						console.log(loginRes)
						user.getopenid({
							code: loginRes.code
						}).then(res1 => {
							// let openid = res.data.data.openid
							// console.log(res1)
							user.getWechatPhone({
								session_key: res1.data.data.session_key,
								iv: e.detail.iv,
								encryptedData: e.detail.encryptedData,
								openid: res1.data.data.openid
							}).then(res => {
								// console.log(res)
								if (res.data.code == 1) {
									_this.$store.commit('getuserinfo', res.data.data)
									// 判断头像是否默认
									// console.log(_this.$store.state.userinfo.avatar.includes('data:'))
									if(_this.$store.state.userinfo.avatar.includes('data:')){//跳转修改页面
										uni.redirectTo({
											url:'/pages/my/infomation'
										})
									}else{
										uni.navigateBack()
									}
									
								}else{
									uni.$u.toast('登录失败，请重新登录')
								}
							})
						})
					}
				})
			},
			nogx() {
				if (!this.isgx) {
					this.$u.toast('请阅读并勾选用户协议')
					return
				}
			},
			clickqt() {
				if (!this.isgx) {
					this.$u.toast('请阅读并勾选用户协议')
					return
				}
				this.to('/pages/my/codelogin')
			}
		}
	}
</script>

<style lang="scss">
	.con {
		padding: 140rpx 50rpx 0;
		display: flex;
		flex-direction: column;
		align-items: center;

		&__logo {
			width: 160rpx;
			height: 160rpx;
			border-radius: 40rpx;
			margin-bottom: 40rpx;
		}

		&__btn {
			width: 650rpx;
			margin-bottom: 100rpx;
			margin-top: 100rpx;
		}

		&__text {
			font-size: 24rpx;
			margin-left: 10rpx;
		}
	}
</style>